package org.atlantis.grosbeak.pot.mapper;

import java.util.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.atlantis.grosbeak.lang.Exceptions;
import org.atlantis.grosbeak.pot.PotException;

/**
 * 把数据库中查询出来的字段的数据映射到一个Date中。
 * 
 * @author <a href="mailto:seenoevil.cn@gmail.com">SeeNoEvil</a>
 * 
 */
public class DateFieldMapper implements FieldMapper<Date> {

	public static final DateFieldMapper ME = new DateFieldMapper();

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.atlantis.grosbeak.pot.mapper.FieldMapper#getTypes()
	 */
	public int[] getTypes() {
		return new int[] { Types.DATE, Types.TIME, Types.TIMESTAMP };
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.atlantis.grosbeak.pot.mapper.FieldMapper#mapField(java.sql.ResultSet,
	 * int, int, boolean)
	 */
	public Date mapField(ResultSet rs, int type, String column, boolean nullable)
			throws SQLException {
		switch (type) {
		case Types.DATE:
			return rs.getDate(column);
		case Types.TIME:
			return rs.getTime(column);
		case Types.TIMESTAMP:
			return rs.getTimestamp(column);
		}
		throw Exceptions.makeThrow(PotException.class,
				"Unable to map SQL type '%d' to java.util.Date.", type);
	}
}
